package com.zxy.supplier_system.server.exception

import org.springframework.boot.web.servlet.error.ErrorAttributes
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
import java.sql.SQLIntegrityConstraintViolationException


@ControllerAdvice
class CustomResponseEntityExceptionHandler(
    private val errorAttributes: ErrorAttributes
) : ResponseEntityExceptionHandler() {

    @ExceptionHandler(
        SQLIntegrityConstraintViolationException::class
    )
    fun handleSQLIntegrityConstraintViolationException(
        ex: RuntimeException,
        request: WebRequest
    ): ResponseEntity<Any>? {
        return handleExceptionInternal(
            ex, null,
            HttpHeaders(), HttpStatus.BAD_REQUEST, request
        )
    }

}